#include "Mysql.h"

int CMysql::ConnectMysql(const char* server, const char* user, const char* password, const char* database)
{
    conn = nullptr;
    conn = mysql_init(nullptr);
    mysql_set_character_set(conn, "gb2312");
    if (!mysql_real_connect(conn, server, user, password, database, 0, nullptr, 0))
    {
        return false;
    }
    return true;
}

int CMysql::SelectMysql(const char* szSql, const int nColumn, std::list<string>& lst)
{
    results = nullptr;
    if (mysql_query(conn, szSql))
    {
        return false;
    }
	results = mysql_store_result(conn);
    if (!results)
    {
        return false;
    }
    while ((record = mysql_fetch_row(results)))
    {
        for (int i = 0; i < nColumn; ++i)
        {
            lst.push_back(record[i]);
        }
    }
    return true;
}

int CMysql::UpdataMysql(const char* szsql)
{
    if (!szsql || !conn)
    {
        return false;
    }
    if (mysql_query(conn, szsql))
    {
        return false;
    }
    return true;
}

void CMysql::DisConnect()
{
    mysql_close(conn);
}